//
//  WPPayFailAlertView.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/10.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPPayFailAlertView.h"

#define DURATION 0.5f

@interface WPPayFailAlertView ()
@property (nonatomic, strong) NSString * failDetail;/**< 失败原因 */
@property (nonatomic, strong) UILabel * failTitleLabel;/**< 失败标题 */
@property (nonatomic, strong) UILabel * failDetailLabel;/**< 失败原因文本 */
@property (nonatomic, strong) UIButton * cancelBtn;/**< 取消 */
@property (nonatomic, strong) UIButton * tryAginBtn;/**< 再试一次 */
@property (nonatomic , strong) UIView * mask;/**< 遮罩视图 */
@property (nonatomic, copy) void(^complete)(NSString * result);/**< block回调 */
- (void)initializeAppearance;/**< 一般界面初始化 */


@end

@implementation WPPayFailAlertView
+ (WPPayFailAlertView *)showViewWithSize:(CGRect)rect andWithDetail:(NSString *)failDetail WithComplete:(void (^)(NSString *))complete {
    WPPayFailAlertView * view = [[WPPayFailAlertView alloc]initWithFrame:rect withDetail:failDetail andWithComplete:complete];
    [view show];
    return view;
}
- (instancetype)initWithFrame:(CGRect)frame withDetail:(NSString *)detail andWithComplete:(void (^)(NSString *))complete {
    if ([super initWithFrame:frame]) {
        _failDetail = detail;
        _complete = complete;
        [self initializeAppearance];
    }
    return self;
}
#pragma mark - init
- (void)initializeAppearance {
    self.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.4];
    [self addSubview:self.cancelBtn];
    [self addSubview:self.failTitleLabel];
    [self addSubview:self.failDetailLabel];
    [self addSubview:self.tryAginBtn];
}
#pragma mark - responds event
- (void)respondsToCancelBtn {
    [self hide];
}
- (void)respondsToTryAgainBtn {
    [self hide];
    _complete(@"再试一次");
}

#pragma mark 显示\隐藏动画
- (void)show {
    [[UIApplication sharedApplication].keyWindow addSubview:self.mask];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.alpha = 0.0;
    self.mask.alpha = 0.0;
    [UIView animateWithDuration:DURATION animations:^{
        self.alpha = 1.0;
    } completion:nil];
}

- (void)hide {
    [UIView animateWithDuration:DURATION animations:^{
        self.alpha = 0.0;
        self.mask.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.mask removeFromSuperview];
    }];
}
#pragma mark - getter
- (UIView *)mask {
    if (!_mask) {
        _mask = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height)];
            view.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.3];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
            [view addGestureRecognizer:tap];
            view;
        });
    }
    return _mask;
}
- (UILabel *)failTitleLabel {
    if (!_failTitleLabel) {
        _failTitleLabel = ({
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(120*DHFlexibleHorizontalMutiplier(), 25*DHFlexibleHorizontalMutiplier(), 200, 40*DHFlexibleHorizontalMutiplier())];
            label.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            label.textColor = [UIColor whiteColor];
            label.text = @"付款失败";
            label;
        });
    }
    return _failTitleLabel;
}
- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = ({
            UIButton * btn =[ UIButton buttonWithType:UIButtonTypeCustom];
            btn.bounds = CGRectMake(0, 0, 22*DHFlexibleHorizontalMutiplier(), 22*DHFlexibleHorizontalMutiplier());
            btn.center = CGPointMake(CGRectGetMaxX(self.bounds)-21*DHFlexibleHorizontalMutiplier(), 20*DHFlexibleHorizontalMutiplier());
            [btn setImage:[UIImage imageNamed:@"alertViewCancel"] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondsToCancelBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _cancelBtn;
}
- (UILabel *)failDetailLabel {
    if (!_failDetailLabel) {
        _failDetailLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), 110*DHFlexibleHorizontalMutiplier(), 200, 30*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(174, 174, 174, 1);
            lab.text = _failDetail;
            lab;
        });
    }
    return _failDetailLabel;;
}
- (UIButton *)tryAginBtn {
    if (!_tryAginBtn) {
        _tryAginBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(50*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.bounds) - 40*DHFlexibleHorizontalMutiplier(), 170*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            btn.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            btn.layer.cornerRadius = 5*DHFlexibleHorizontalMutiplier();
            [btn setTitle:@"再试一次" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            [btn addTarget:self action:@selector(respondsToTryAgainBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;

        });
    }
    return _tryAginBtn;
}
@end
